/*
 * $Id: SelectedPhraseOutManager.java 8 2012-04-28 05:11:23Z forwardly@gmail.com $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.as.service.category;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.as.dao.category.SelectedPhraseOutDao;
import com.lightsaber.trade.as.entity.category.CategoryLeaf;
import com.lightsaber.trade.as.entity.category.SelectedPhraseOut;
import com.lightsaber.trade.as.entity.programme.Programme;
import com.lightsaber.trade.as.service.programme.ProgrammeManager;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.service.BaseServiceImpl;

@Service
@Transactional
public class SelectedPhraseOutManager extends BaseServiceImpl<SelectedPhraseOut, String> {
    final static Logger LOG = LoggerFactory.getLogger(SelectedPhraseOutManager.class);

    @Autowired
    private SelectedPhraseOutDao selectedPhraseOutDao;
    @Autowired
    private CategoryNodeService categoryNodeService;
    @Autowired
    private ProgrammeManager programmeManager;

    @Override
    protected BaseDao<SelectedPhraseOut, String> getDao() {
        return selectedPhraseOutDao;
    }

    public void delete(final List<String> ids) {
        if (ids == null || ids.size() <= 0) {
            return;
        }

        for (String id : ids) {
            super.delete(id);
        }

    }

    public List<SelectedPhraseOut> findByCategoryLeafId(final String categoryLeafId) {
        return selectedPhraseOutDao.findByCategoryLeafId(categoryLeafId);
    }

    public void save(final List<String> progList, final String categoryNodeId) {
        if (progList != null) {
            CategoryLeaf leaf = categoryNodeService.findLeafById(categoryNodeId);
            for (String prog : progList) {
                LOG.info("programme id : {}", prog);
                List<SelectedPhraseOut> list = selectedPhraseOutDao.findByLeafIdAndProgId(
                        leaf.getId(), prog);
                if (!list.isEmpty()) {
                    continue;
                }
                Programme programme = programmeManager.get(prog);
                SelectedPhraseOut out = new SelectedPhraseOut();
                out.setCategoryLeaf(leaf);
                out.setProgramme(programme);
                selectedPhraseOutDao.save(out);
            }
        }
    }
}
